package com.sghd.candy.module.box.manager;

import com.sghd.common.ramcache.anno.Inject;
import com.sghd.common.ramcache.service.EntityCacheService;
import com.sghd.pojo.candy.box.enums.ScopeType;
import org.springframework.stereotype.Component;

import java.util.Comparator;
import java.util.List;

/**
 * @author Allen
 * @date 2018/12/17
 */
@Component
public class BoxKeysConfigManager {
    @Inject
    private EntityCacheService<Integer, BoxKeysConfig> boxKeysConfigCache;

    public BoxKeysConfig getBoxKeysConfig(int keyId) {
        return boxKeysConfigCache.load(keyId);
    }

    void config(BoxKeysConfig boxKeysConfig, float dropRate) {
        boxKeysConfig.config(dropRate);
    }

    void config(BoxKeysConfig boxKeysConfig, ScopeType scopeType) {
        boxKeysConfig.config(scopeType);
    }

    void config(BoxKeysConfig boxKeysConfig, int isValid) {
        boxKeysConfig.config(isValid);
    }

    public List<BoxKeysConfig> findAll(ScopeType scopeType) {
        return boxKeysConfigCache.getFinder().find(
                filter ->
                        filter.getIsValid() == 0 ||
                                !filter.getScopeType().contains(scopeType),
                Comparator.comparingInt(BoxKeysConfig::getKeyId));
    }
}
